package com.zhenmyan.secdeal.controller;
import com.zhenmyan.secdeal.Enum.ResponseEnum;
import com.zhenmyan.secdeal.service.UserService;
import com.zhenmyan.secdeal.vo.LoginRequest;
import com.zhenmyan.secdeal.vo.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Objects;

/**
 * description:
 *
 * @author 颜真明
 * @date 2023/5/5  14:55
 */
@Controller
@RequestMapping("/login")
@Slf4j
public class LoginController {

    @Resource
    private UserService userService;

    @RequestMapping("/toLogin")
    public String log() {
        return "login";
    }

    /**
     * 登录功能
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
    @ResponseBody
    public Response doLogin(@Valid LoginRequest loginRequest, HttpServletRequest request, HttpServletResponse response) {
        if (Objects.isNull(request) || StringUtils.isEmpty(loginRequest.getMobile()) || StringUtils.isEmpty(loginRequest.getPassword())) {
            return Response.error(ResponseEnum.LOGIN_ERROR);
        }
        log.info("LoginController::request is {}", request);
        return userService.doLogin(loginRequest, request, response);
    }

}
